<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateQrcodeAPIRequest;
use App\Http\Requests\API\UpdateQrcodeAPIRequest;
use App\Models\Qrcode;
use App\Repositories\QrcodeRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class QrcodeController
 * @package App\Http\Controllers\API
 */

class QrcodeAPIController extends AppBaseController
{
    /** @var  QrcodeRepository */
    private $qrcodeRepository;

    public function __construct(QrcodeRepository $qrcodeRepo)
    {
        $this->qrcodeRepository = $qrcodeRepo;
    }

    /**
     * Display a listing of the Qrcode.
     * GET|HEAD /qrcodes
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->qrcodeRepository->pushCriteria(new RequestCriteria($request));
        $this->qrcodeRepository->pushCriteria(new LimitOffsetCriteria($request));
        $qrcodes = $this->qrcodeRepository->all();

        return $this->sendResponse($qrcodes->toArray(), 'Qrcodes retrieved successfully');
    }

    /**
     * Store a newly created Qrcode in storage.
     * POST /qrcodes
     *
     * @param CreateQrcodeAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateQrcodeAPIRequest $request)
    {
        $input = $request->all();

        $qrcodes = $this->qrcodeRepository->create($input);

        return $this->sendResponse($qrcodes->toArray(), 'Qrcode saved successfully');
    }

    /**
     * Display the specified Qrcode.
     * GET|HEAD /qrcodes/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var Qrcode $qrcode */
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            return $this->sendError('Qrcode not found');
        }

        return $this->sendResponse($qrcode->toArray(), 'Qrcode retrieved successfully');
    }

    /**
     * Update the specified Qrcode in storage.
     * PUT/PATCH /qrcodes/{id}
     *
     * @param  int $id
     * @param UpdateQrcodeAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateQrcodeAPIRequest $request)
    {
        $input = $request->all();

        /** @var Qrcode $qrcode */
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            return $this->sendError('Qrcode not found');
        }

        $qrcode = $this->qrcodeRepository->update($input, $id);

        return $this->sendResponse($qrcode->toArray(), 'Qrcode updated successfully');
    }

    /**
     * Remove the specified Qrcode from storage.
     * DELETE /qrcodes/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var Qrcode $qrcode */
        $qrcode = $this->qrcodeRepository->findWithoutFail($id);

        if (empty($qrcode)) {
            return $this->sendError('Qrcode not found');
        }

        $qrcode->delete();

        return $this->sendResponse($id, 'Qrcode deleted successfully');
    }
}
